# coding=utf-8
# 2019/12/12: 首个记录
# 2020/4/1: 导入bi_common

import bi_common as bi


# 天气类型
class WeatherType:
    UNKNOWN = 0  # 未知
    SUNNY = 1  # 晴
    CLOUDY = 2  # 阴
    RAINY = 3  # 雨
    SNOWY = 4  # 雪
    FOGGY = 5  # 雾
    SAND = 6  # 沙尘

    def __init__(self):
        pass


# 道路类型
class RoadType:
    UNKNOWN = 0  # 未知
    HIGHWAY = 1  # 高速路，无红绿灯，80~120KPH
    CITY_EXPRESS = 2  # 城市快速路，无红绿灯，60~80KPH
    CITY_MAIN = 3  # 城市主干道，有红绿灯，推荐60KPH
    GENERAL_ROAD = 4  # 一般道路，有红绿灯，推荐40KPH
    COUNTRY_ROAD = 5  # 乡村道路，无红绿灯，~40KPH

    def __init__(self):
        pass


# 场所类型
class SpotType:
    UNKNOWN = 0  # 收费站
    TOLL_GATE = 1  # 不响
    SERVICE_AREA = 2  # 服务区
    TUNNEL = 3  # 隧道
    ACCIDENT = 4  # 事故
    CROSSING = 5  # 路口
    ROUNDABOUT = 7  # 环岛
    HIGHWAY_ENTRANCE = 8  # 高速入口
    HIGHWAY_EXIT = 9  # 高速出口
    BRIDGE = 10  # 桥
    EXPRESS_ENTRANCE = 11  # 快速路入口
    EXPRESS_EXIT = 12  # 快速路出口

    def __init__(self):
        pass


# 场所信息
class Spot:
    def __init__(self):
        self.spot_type = SpotType.UNKNOWN  # 场所类型
        self.distance = 0.0  # 到场所的距离 m


# 交通状况
class TrafficStatus:
    UNKNOWN = 0  # 未知
    OPEN = 1  # 畅通
    SLOW = 2  # 缓慢
    CROWDED = 3  # 拥挤

    def __init__(self):
        pass


# 环境信息样本
class EnvSample:
    def __init__(self):
        self.time = 0.0  # 时间戳 s
        self.weather_type = WeatherType.UNKNOWN  # 天气类型
        self.temperature_valid = False
        self.temperature = 0.0  # 温度 °C
        self.pm25_valid = False
        self.pm25 = 0.0  # ug/m3
        self.road_name = None  # 道路名称
        self.speed_limit_valid = False
        self.speed_limit = 0  # 道路限速 KPH
        self.road_type = RoadType.UNKNOWN  # 道路类型
        self.illumination_valid = False
        self.illumination = 0.0  # 光照强度 lux
        self.wind_speed_valid = False
        self.wind_speed = 0.0  # 风速 m/s
        self.traffic_status = TrafficStatus.UNKNOWN  # 交通状况
        self.spots = []  # 场所列表

    # 转通用样本，用于样本输出
    def to_general_sample(self):
        output = bi.agency.create_general_sample()
        output.protocol = "env-sample-v2"
        output.time = self.time
        road_name_bin = bytearray(self.road_name, encoding='utf-8') if self.road_name is not None else []
        output.significant = 10 + len(self.spots) * 2 + len(road_name_bin)
        output.values = []
        output.values.append(len(road_name_bin))
        output.values.append(len(self.spots))
        output.values.append(self.weather_type)
        output.values.append(self.temperature if self.temperature_valid else None)
        output.values.append(self.pm25 if self.pm25_valid else None)
        output.values.append(self.speed_limit if self.speed_limit_valid else None)
        output.values.append(self.road_type)
        output.values.append(self.illumination if self.illumination_valid else None)
        output.values.append(self.wind_speed if self.wind_speed_valid else None)
        output.values.append(self.traffic_status)
        for spot in self.spots:
            if isinstance(spot, Spot):
                output.values.append(spot.spot_type)
                output.values.append(spot.distance)
            else:
                output.values.append(SpotType.UNKNOWN)
                output.values.append(10000.0)
        for b in road_name_bin:
            output.values.append(int(b))
        return output


def _conv_env_sample_v2(gs):
    values_count = len(gs.values)
    if values_count < 2:
        return None
    road_name_bin_size = int(gs.values[0]) if gs.values[0] is not None else 0
    spot_count = int(gs.values[1]) if gs.values[1] is not None else 0
    if values_count != 10 + 2 * spot_count + road_name_bin_size:
        return None
    output = EnvSample()
    output.time = gs.time
    output.weather_type = int(gs.values[2]) if gs.values[2] is not None else WeatherType.UNKNOWN
    if gs.values[3] is not None:
        output.temperature_valid = True
        output.temperature = float(gs.values[3])
    if gs.values[4] is not None:
        output.pm25_valid = True
        output.pm25 = float(gs.values[4])
    if gs.values[5] is not None:
        output.speed_limit_valid = True
        output.speed_limit = int(gs.values[5])
    output.road_type = int(gs.values[6]) if gs.values[6] is not None else RoadType.UNKNOWN
    if gs.values[7] is not None:
        output.illumination_valid = True
        output.illumination = float(gs.values[7])
    if gs.values[8] is not None:
        output.wind_speed_valid = True
        output.wind_speed = float(gs.values[8])
    output.traffic_status = int(gs.values[9]) if gs.values[9] is not None else TrafficStatus.UNKNOWN
    for i in range(0, spot_count):
        if gs.values[10 + 2 * i] is not None and gs.values[11 + 2 * i] is not None:
            spot = Spot()
            spot.spot_type = int(gs.values[10 + 2 * i])
            spot.distance = float(gs.values[11 + 2 * i])
            output.spots.append(spot)
    road_name_bin_ints = []
    for i in range(0, road_name_bin_size):
        b = gs.values[10 + 2 * spot_count + i]
        if b is None:
            road_name_bin_ints = []
            break
        else:
            road_name_bin_ints.append(int(b))
    if len(road_name_bin_ints) > 0:
        output.road_name = bytes(road_name_bin_ints).decode('utf-8')
    return output


# 获取EnvSample，用于样本输入
def get_env_sample():
    s1 = None
    s2 = None
    w1 = 0.0
    w2 = 0.0
    if 'env-sample-v2' in bi.input_samples:
        pair = bi.input_samples['env-sample-v2']
        s1 = _conv_env_sample_v2(pair.sample1)
        w1 = pair.weight1
        s2 = _conv_env_sample_v2(pair.sample2)
        w2 = pair.weight2
    if s1 is not None and s2 is not None:
        return s1 if w1 > w2 else s2
    return None
